package com.cskaoyan.user.service.impl;

import com.alibaba.fastjson.JSON;
import com.cskaoyan.mall.commons.util.jwt.JwtTokenUtils;
import com.cskaoyan.user.constants.UserRetCode;
import com.cskaoyan.user.converter.MemberConverter;
import com.cskaoyan.user.dal.entitys.Member;
import com.cskaoyan.user.dal.entitys.UserVerify;
import com.cskaoyan.user.dal.persistence.MemberMapper;
import com.cskaoyan.user.dal.persistence.UserVerifyMapper;
import com.cskaoyan.user.dto.UserLoginRequest;
import com.cskaoyan.user.dto.UserLoginResponse;
import com.cskaoyan.user.dto.UserLoginVerifyResponse;
import com.cskaoyan.user.service.IUserLogin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import tk.mybatis.mapper.entity.Example;


import java.util.List;
import java.util.Map;

@Service
public class UserLoginImpl implements IUserLogin {
    @Autowired
    MemberMapper memberMapper;
    @Autowired
    MemberConverter memberConverter;
    @Autowired
    UserVerifyMapper userVerifyMapper;
    @Override
    public UserLoginResponse login(UserLoginRequest userLoginRequest) {
        UserLoginResponse userLoginResponse = new UserLoginResponse();

        //校验参数
        userLoginRequest.requestCheck();
//
        //先查数据库
        Example example = new Example(Member.class);
        example.createCriteria().andEqualTo("username",userLoginRequest.getUserName()).andEqualTo("");
        List<Member> members = memberMapper.selectByExample(example);
        //查到列表不唯一或者为空
        if (members.size()==0||members.size()>1){
            userLoginResponse.setMsg(UserRetCode.DATA_NOT_EXIST.getMessage());
            userLoginResponse.setCode(UserRetCode.DATA_NOT_EXIST.getCode());
            return userLoginResponse;
        }
        //拿到用户对象
        Member member = members.get(0);
        //查看是否激活
        String isVerified = member.getIsVerified();
        if (isVerified == "N") {
            userLoginResponse.setMsg(UserRetCode.USER_ISVERFIED_ERROR.getMessage());
            userLoginResponse.setCode(UserRetCode.USER_ISVERFIED_ERROR.getCode());
            return userLoginResponse;
        }
        //用户激活了
        UserLoginResponse userLoginResponse1 = memberConverter.member2UserLoginResponse(member);
        //得出token
        //查看密码是否正确
        String s = DigestUtils.md5DigestAsHex(userLoginRequest.getPassword().getBytes());
        if (!s.equals(member.getPassword())){
            userLoginResponse.setMsg(UserRetCode.USERORPASSWORD_ERRROR .getMessage());
            userLoginResponse.setCode(UserRetCode.USERORPASSWORD_ERRROR .getCode());
            return userLoginResponse;
        }
        String token = JwtTokenUtils.builder().msg(JSON.toJSONString(member)).build().creatJwtToken();

        userLoginResponse1.setToken(token);
        userLoginResponse1.setCode(UserRetCode. SUCCESS.getCode());
        userLoginResponse1.setMsg(UserRetCode. SUCCESS.getMessage());
        return  userLoginResponse1;
    }

    @Override
    public UserLoginVerifyResponse loginVerify(String token) {

        UserLoginVerifyResponse userLoginVerifyResponse = new UserLoginVerifyResponse();


        Map map = JSON.parseObject(token, Map.class);
        Example example = new Example(UserVerify.class);
         example.createCriteria().andEqualTo("username", map.get("username")).andEqualTo("isExpire","N");

        List<UserVerify> userVerifies = userVerifyMapper.selectByExample(example);
        //拿到userVerifie对象
        if (userVerifies.size()==0||userVerifies.size()>1){
            userLoginVerifyResponse.setMsg(UserRetCode.DATA_NOT_EXIST.getMessage());
            userLoginVerifyResponse.setCode(UserRetCode.DATA_NOT_EXIST.getCode());
            return userLoginVerifyResponse;
        }
        UserVerify userVerify = userVerifies.get(0);
        String isVerify = userVerify.getIsVerify();
        if (isVerify == "N") {
            userLoginVerifyResponse.setMsg(UserRetCode.USER_ISVERFIED_ERROR.getMessage());
            userLoginVerifyResponse.setCode(UserRetCode.USER_ISVERFIED_ERROR.getCode());
            return userLoginVerifyResponse;
        }
        String  id = map.get("id").toString();
        userLoginVerifyResponse.setUid(id);
        userLoginVerifyResponse.setCode(UserRetCode.SUCCESS.getCode());
        userLoginVerifyResponse.setMsg(UserRetCode.SUCCESS.getMessage());

        String username = (String) map.get("username");
        userLoginVerifyResponse.setUsername(username);
        String file = (String) map.get("file");
        if (file!=null){
            userLoginVerifyResponse.setFile(file);
        }

        return userLoginVerifyResponse;

    }
}
